package com.macrounion.atv.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.macrounion.atv.service.entity.User;
import com.macrounion.base.service.utils.SessionHandler;

//import com.macrounion.llt.service.entity.User;

@Controller
public class HomeController {

	@Autowired
	protected HttpServletRequest request;

	@RequestMapping(value={"/",""},method=RequestMethod.GET)
	public ModelAndView home(){
		return new ModelAndView("redirect:/web/index");
	}
	
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public ModelAndView login(){
		SessionHandler.setContext(null);
		return new ModelAndView("login");
	}

	@RequestMapping(value="/logout",method=RequestMethod.GET)
	public ModelAndView logout(){
		SessionHandler.setContext(null);
		request.getSession().invalidate();
		return new ModelAndView("login");
	}
	
	@RequestMapping(value={"/web/index"},method=RequestMethod.GET)
	public ModelAndView index(){
		ModelAndView mv = new ModelAndView("index");
		User user = (User)SessionHandler.getContext();
		if (user ==null)
			return new ModelAndView("login");
		mv.addObject("User", user);
		return mv;
	}

}
